package yl.hs.bmipfull.security;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import yl.hs.bmipfull.utils.ResultCode;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.WebResult;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * SecurityAuthenticationEntryPoint
 * @category
 * @author 谢景广
 * @date 2021-05-26
 */
public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        if (e != null) {
            var webResult = new WebResult(ResultCode.UnAuthentication, "需要登录", request.getRequestURL().toString());
            Tl.writeObjectToResponse(response, webResult);
        }
    }
}
